﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace manySquares
{
    class Board
    {
        Spaces[,] board;
        public int x;
        public int y;
        public Board(int ix, int iy)
        {
            board = new Spaces[ix, iy];
            x = ix;
            y = iy;
            for (int a = 0; a < x; a++)
            {
                for (int b = 0; b < y; b++)
                {
                    board[a, b] = new Spaces(0, 0, 0, 0,"None");
                }
            }
        }
        public void changeRect(int x, int y)
        {
            board[x, y].rec = new Rectangle(1, 1, 40, 40);
        }
        public void resetRect(int x, int y)
        {
            board[x, y].rec = new Rectangle(1, 247, 40, 40);
        }
        public void addUnit(int x, int y)
        {
            board[x, y].unitType = 1;
            board[x, y].setUnit();
            board[x, y].rec = new Rectangle(1, 1, 40, 40);
        }
        public void removeUnit(int x, int y)
        {
            board[x, y].unitType = 0;
            board[x, y].rec = new Rectangle(1, 247, 40, 40);
            board[x, y].removeUnit();
        }
        public void resetAll()
        {
            for (int a = 0; a < x; a++)
            {
                for (int b = 0; b < y; b++)
                {
                    board[a, b].color = Color.White;
                }
            }
        }
        public void selectSpace(int x, int y,Color cl)
        {
            board[x, y].color = cl;
            
            if (board[x, y].unitType == 1)
            {
                if (x > 0)
                {
                    //if(board[x-1,y].unitType==0)
                      //  board[x - 1, y].color = Color.Green;
                    for (int r = board[x, y].unit.getSpeed(); r > 0; r--)
                    {
                        if (x >=r)
                            if (board[x - r, y].unitType == 0)
                                board[x - r, y].color = Color.Green;
                    }
                    for (int r = board[x, y].unit.getRange(); r > 0; r--)
                    {
                        if(x>=r)
                            if (board[x - r, y].unitType == 1)
                                board[x - r, y].color = Color.Red;
                    }
                }
                if (x <this.x-1)
                {
                    for (int r = board[x, y].unit.getSpeed(); r > 0; r--)
                    {
                        if (x < this.x - r)
                            if (board[x + r, y].unitType == 0)
                                board[x + r, y].color = Color.Green;
                    }
                    for (int r = board[x,y].unit.getRange(); r > 0; r--)
                    {
                        if (x < this.x-r)
                            if (board[x + r, y].unitType == 1)
                                board[x + r, y].color = Color.Red;
                    }
                } 
                if (y > 0)
                {
                    for (int r = board[x, y].unit.getSpeed(); r > 0; r--)
                    {
                        if (y >= r)
                            if (board[x, y-r].unitType == 0)
                                board[x, y-r].color = Color.Green;
                    }
                    for (int r = board[x, y].unit.getRange(); r > 0; r--)
                    {
                        if (y >= r)
                            if (board[x, y-r].unitType == 1)
                                board[x, y-r].color = Color.Red;
                    }
                }
                if (y <this.y-1)
                {
                    for (int r = board[x, y].unit.getSpeed(); r > 0; r--)
                    {
                        if (y < this.y - r)
                            if (board[x, y+r].unitType == 0)
                                board[x, y+r].color = Color.Green;
                    }
                    for (int r = board[x, y].unit.getRange(); r > 0; r--)
                    {
                        if (y < this.y - r)
                            if (board[x, y+r].unitType == 1)
                                board[x, y+r].color = Color.Red;
                    }
                }
            }
        }
        public Rectangle GetSourceRect(int x, int y)
        {
            return board[x, y].GetSourceRect();
        }
        public Color GetColor(int x, int y)
        {
            return board[x, y].GetColor();
        }
        public int GetUnit(int x, int y)
        {
            return board[x, y].unitType;
        }
    }
}
